package com.pig4cloud.pigx.wallet.api.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.api.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.api.util.OpenApiUtil;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.wallet.api.v1.service.ContractAccountDetailServiceV1;
import com.pig4cloud.pigx.wallet.api.v1.utils.WalletUtilsV1;
import com.pig4cloud.pigx.wallet.entity.ContractAccountDetail;
import com.pig4cloud.pigx.wallet.enums.WalletContractAccountDetailEnum;
import com.pig4cloud.pigx.wallet.mapper.ContractAccountDetailMapper;
import com.pig4cloud.pigx.wallet.open.api.dto.OpenApiContractAccountDetailDTOV1;
import com.pig4cloud.pigx.wallet.open.api.vo.OpenApiContractAccountDetailVOV1;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 */
@Service
@AllArgsConstructor
public class ContractAccountDetailServiceImplV1 extends ServiceImpl<ContractAccountDetailMapper, ContractAccountDetail> implements ContractAccountDetailServiceV1 {

	private final WalletUtilsV1 utils;

	/**
	 * 获取账户损益资金流水
	 *
	 * @param dto dto
	 * @return 流水
	 */
	@Override
	public R<Page<OpenApiContractAccountDetailVOV1>> getUserList(OpenApiContractAccountDetailDTOV1 dto) {
		// 分页
		Page<ContractAccountDetail> page = new Page<>();
		if (dto.getCurrent() == null) {
			page.setCurrent(1);
		} else {
			page.setCurrent(dto.getCurrent());
		}
		if (dto.getSize() == null) {
			page.setSize(10);
		} else {
			page.setSize(dto.getSize());
		}
		// 币对校验
		utils.getSymbolMongo(dto.getSymbolName());
		// 获取用户
		PigxUser user = utils.getUser();
		try {
			Page<ContractAccountDetail> selectPage = baseMapper.selectPage(page, new QueryWrapper<ContractAccountDetail>() {{
				eq("user_id", user.getId());
				eq("symbol_name", dto.getSymbolName());
				eq("business_type", WalletContractAccountDetailEnum.BusinessType.PROFIT.getValue());
				if (dto.getStartTime() != null) {
					ge("create_time", Instant.ofEpochMilli(dto.getStartTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
				}
				if (dto.getEndTime() != null) {
					le("create_time", Instant.ofEpochMilli(dto.getEndTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime());
				}
				orderByDesc("create_time");
			}});
			// 类型转换
			Page<OpenApiContractAccountDetailVOV1> voPage = new Page<>();
			BeanUtils.copyProperties(selectPage, voPage);
			List<OpenApiContractAccountDetailVOV1> list = new ArrayList<>();
			for (ContractAccountDetail record : selectPage.getRecords()) {
				OpenApiContractAccountDetailVOV1 vo = new OpenApiContractAccountDetailVOV1();
				// 根据操作加减对盈亏进行正负赋值
				if (WalletContractAccountDetailEnum.OperationType.ADD.getValue().equals(record.getOperationType())) {
					vo.setAmount(record.getAmount());
				} else {
					vo.setAmount(record.getAmount().negate());
				}
				vo.setTime(record.getCreateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
				vo.setSymbolName(dto.getSymbolName());
				list.add(vo);
			}
			voPage.setRecords(list);
			return R.ok(voPage);
		} catch (Exception e) {
			throw OpenApiUtil.getError(AbnormalEnum.MSG.DISCONNECTED);
		}
	}

}
